#include <bits/stdc++.h>
#define int long long
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
k--;
vector<int> ft(min(n+1, 20LL));
ft[0] = 1;
for(int i = 1; i < ft.size(); i++) {
ft[i] = ft[i-1] * i;
}
if(ft.back() <= k) {
cout << "-1\n";
return 0;
}
vector<int> a;
for(int i = ft.size()-1, j = k; i >= 1; i--) {
a.push_back(j / ft[i-1]);
j = j % ft[i-1];
}
vector<int> last;
for(int i = 0; i < a.size(); i++) {
last.push_back(n-a.size()+i+1);
}
for(int i = 0; i < last.size(); i++) {
swap(last[i], last[i+a[i]]);
sort(last.begin()+i+1, last.end());
}
function<int(int)> f = [&] (int i) {
if(i < n - last.size() + 1) {
return i;
}
else {
return last[i - (n - last.size() + 1)];
}
};
vector<int> lucky;
for(int d = 1; d <= 9; d++) {
for(int mask = 0; mask < (1 << d); mask++) {
int num = 0;
for(int i = 0; i < d; i++) {
if(mask & (1 << i)) {
num = num * 10 + 7;
}
else {
num = num * 10 + 4;
}
}
lucky.push_back(num);
}
}
sort(lucky.begin(), lucky.end());
int cnt = 0;
for(int i : lucky) {
if(i > n)
break;
int j = f(i);
if(binary_search(lucky.begin(), lucky.end(), j)) {
cnt++;
}
}
cout << cnt << "\n";
}
1466C - Canine poetry | 74A - Room Leader |
1333D - Challenges in school №41 | 1475B - New Year's Number |
461A - Appleman and Toastman | 320B - Ping-Pong (Easy Version) |
948A - Protect Sheep | 387A - George and Sleep |
53A - Autocomplete | 1729G - Cut Substrings |
805B - 3-palindrome | 805C - Find Amir |
676C - Vasya and String | 1042B - Vitamins |
1729F - Kirei and the Linear Function | 25D - Roads not only in Berland |
1694A - Creep | 659F - Polycarp and Hay |
1040A - Palindrome Dance | 372A - Counting Kangaroos is Fun |
1396B - Stoned Game | 16A - Flag |
1056A - Determine Line | 670B - Game of Robots |
1418C - Mortal Kombat Tower | 1382B - Sequential Nim |
1272C - Yet Another Broken Keyboard | 808A - Lucky Year |
1245A - Good ol' Numbers Coloring | 58B - Coins |